﻿
Imports KUeLib.Buchungen
Imports System.Windows.Forms
Imports System.Drawing
Imports KUeLib.Data.ServiceDefinitions
Imports KUeLib.Data

Namespace Formulare.Buchungen

    Public Class BuchunguebersichtForm
        Inherits WeifenLuo.WinFormsUI.Docking.DockContent
        Implements LibMultiPlug.Core.Plugins.Interfaces.IFormOptions

        Private _kuService As BuchungenDataSerivce
        Private _buchungen As List(Of Buchung)
        Private _childForms As New List(Of BuchungDetailForm)


#Region "IFormOptions Implementation"

        Public Sub Delete() Implements LibMultiPlug.Core.Plugins.Interfaces.IFormOptions.Delete

        End Sub

        Public Sub [New]() Implements LibMultiPlug.Core.Plugins.Interfaces.IFormOptions.New
            Dim nb As New BuchungDetailForm
            nb.Show(Me.DockPanel)
        End Sub

        Public Sub Open() Implements LibMultiPlug.Core.Plugins.Interfaces.IFormOptions.Open
            Call OpenSelected()
        End Sub

        Public Event OptionsChanged(ByVal sender As Object, ByVal e As System.EventArgs) Implements LibMultiPlug.Core.Plugins.Interfaces.IFormOptions.OptionsChanged

        Public Sub PagePreview() Implements LibMultiPlug.Core.Plugins.Interfaces.IFormOptions.PagePreview

        End Sub

        Public Sub Print() Implements LibMultiPlug.Core.Plugins.Interfaces.IFormOptions.Print

        End Sub

        Public Sub Refresh1() Implements LibMultiPlug.Core.Plugins.Interfaces.IFormOptions.Refresh
            Call InitializeList()
        End Sub

        Public Sub Save() Implements LibMultiPlug.Core.Plugins.Interfaces.IFormOptions.Save

        End Sub

        Public Sub SaveAs() Implements LibMultiPlug.Core.Plugins.Interfaces.IFormOptions.SaveAs

        End Sub

        Public ReadOnly Property SetCoopDesign As Boolean Implements LibMultiPlug.Core.Plugins.Interfaces.IFormOptions.SetCoopDesign
            Get
                Return True
            End Get
        End Property

        Public ReadOnly Property SetDelete As Boolean Implements LibMultiPlug.Core.Plugins.Interfaces.IFormOptions.SetDelete
            Get

            End Get
        End Property

        Public ReadOnly Property SetNew As Boolean Implements LibMultiPlug.Core.Plugins.Interfaces.IFormOptions.SetNew
            Get
                Return True
            End Get
        End Property

        Public ReadOnly Property SetOpen As Boolean Implements LibMultiPlug.Core.Plugins.Interfaces.IFormOptions.SetOpen
            Get
                Return (lvBuchungen.SelectedIndices.Count > 0)
            End Get
        End Property

        Public ReadOnly Property SetPagePreview As Boolean Implements LibMultiPlug.Core.Plugins.Interfaces.IFormOptions.SetPagePreview
            Get

            End Get
        End Property

        Public ReadOnly Property SetPrint As Boolean Implements LibMultiPlug.Core.Plugins.Interfaces.IFormOptions.SetPrint
            Get

            End Get
        End Property

        Public ReadOnly Property SetRefresh As Boolean Implements LibMultiPlug.Core.Plugins.Interfaces.IFormOptions.SetRefresh
            Get
                Return True
            End Get
        End Property

        Public ReadOnly Property SetSave As Boolean Implements LibMultiPlug.Core.Plugins.Interfaces.IFormOptions.SetSave
            Get

            End Get
        End Property

        Public ReadOnly Property SetSaveAs As Boolean Implements LibMultiPlug.Core.Plugins.Interfaces.IFormOptions.SetSaveAs
            Get

            End Get
        End Property

#End Region


        Private Sub BuchungenUebersichtForm_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.GotFocus
            InitializeList()
        End Sub

        Private Sub AusgabenUebersichtForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            _kuService = ServiceManager.GetInstance().GetBuchungenDataService()

            Call InitializeForm()

        End Sub

        Private Sub InitializeForm()

            Call InitializeList()

        End Sub

        Private Sub InitializeList()

            Me.SuspendLayout()

            lvBuchungen.Items.Clear()

            _buchungen = _kuService.GetAlleBuchungen()
            _buchungen.Sort(New BuchungComparer)

            If _buchungen IsNot Nothing Then
                For Each b As Buchung In _buchungen

                    Dim lvi As ListViewItem = lvBuchungen.Items.Add(b.Buchungsdatum.ToShortDateString())
                    lvi.SubItems.Add(b.Wertstellungsdatum.ToShortDateString())

                    Dim lvsi As System.Windows.Forms.ListViewItem.ListViewSubItem = _
                    lvi.SubItems.Add(b.Betrag.ToString("C", (New System.Globalization.CultureInfo("de-DE").NumberFormat)))

                    lvsi.BackColor = Color.Silver

                    lvi.SubItems.Add(b.Kontostand.ToString("C", (New System.Globalization.CultureInfo("de-DE").NumberFormat)))
                    lvi.SubItems.Add(b.Buchungstext)
                    lvi.SubItems.Add(b.Beguenstigter)
                    lvi.SubItems.Add(b.Verwendungszweck(0))
                    lvi.SubItems.Add(b.Bemerkung)
                    lvi.SubItems.Add(b.Status)

                    'If b.Betrag >= 0 Then
                    '    lvi.BackColor = Color.LightGreen
                    'Else
                    '    lvi.BackColor = Color.LightCoral
                    'End If

                Next

                For i As Integer = 0 To lvBuchungen.Columns.Count - 1 Step 1
                    lvBuchungen.Columns(i).Width = -1
                    lvBuchungen.Columns(i).Width += 10
                Next

            End If

            Me.ResumeLayout()

        End Sub

        Private Sub OpenSelected()
            For Each openedBdf As BuchungDetailForm In _childForms
                If Not openedBdf.IsDisposed Then _
                    If openedBdf.Buchung.Id = _buchungen(lvBuchungen.SelectedIndices(0)).Id Then _
                        openedBdf.BringToFront() : Exit Sub
            Next

            Dim bdf As New BuchungDetailForm()
            _childForms.Add(bdf)


            bdf.Show(Me.DockPanel, _buchungen(lvBuchungen.SelectedIndices(0)).Id)
        End Sub

        Private Sub lvBuchungen_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lvBuchungen.MouseDoubleClick



            Call OpenSelected()


        End Sub


        Private Sub lvBuchungen_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvBuchungen.SelectedIndexChanged

            Call SetCurrentBuchungStatusText()

            RaiseEvent OptionsChanged(Me, Nothing)

        End Sub

        Private Sub SetCurrentBuchungStatusText()

            Try

                Dim currentBuchung As Buchung _
                    = _buchungen(lvBuchungen.SelectedIndices(0))

                Dim message As String _
                    = String.Format("Buchung vom {0} über {1} €; {2}", currentBuchung.Buchungsdatum.ToShortDateString(), _
                                                                       currentBuchung.Betrag.ToString("C", New System.Globalization.CultureInfo("de-DE").NumberFormat), _
                                                                       currentBuchung.Buchungstext)
                LibMultiPlug.Core.Statusmeldungen.Statusmeldungscontroller.SetLabelText(message)

            Catch ex As Exception
            End Try

        End Sub



    End Class

End Namespace